//
//  RootTabBarController.m
//  CD1505WeiBo
//
//  Created by 千锋 on 15/12/29.
//  Copyright (c) 2015年 LiChaoRan. All rights reserved.
//

#import "RootTabBarController.h"
#import "HomeTableViewController.h"
#import "MessageTableViewController.h"
#import "DiscoveryTableViewController.h"
#import "PersonalTableViewController.h"

#import <PopMenu.h>

#import "WBTabBar.h"

@interface RootTabBarController ()<WBtabBarDelegate>

@property (nonatomic, strong)WBTabBar *wbTabBar;//自定义tabbar

@property (nonatomic, strong)PopMenu *popMenu;//弹出界面

@end

@implementation RootTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor=[UIColor orangeColor];
    
    //添加自定义tabbar
    [self addCustomTabBar];
    
    //添加viewcontrollers
    [self addViewControllers];
}

//懒加载
- (PopMenu *)popMenu{
    if (_popMenu==nil) {
        NSMutableArray *items=[NSMutableArray array];
        NSArray *titleArr=@[@"文字",@"相册",@"拍摄",@"签到",@"点评",@"更多"];
        NSArray *imgNameArr=@[@"tabbar_compose_idea",
                              @"tabbar_compose_photo",
                              @"tabbar_compose_weibo",
                              @"tabbar_compose_lbs",
                              @"tabbar_compose_review",
                              @"tabbar_compose_more"];
        for (int i=0; i<titleArr.count; i++) {
            MenuItem *item=[[MenuItem alloc]initWithTitle:titleArr[i] iconName:imgNameArr[i] glowColor:[UIColor magentaColor]];
            [items addObject:item];
        }
        _popMenu=[[PopMenu alloc]initWithFrame:[UIScreen mainScreen].bounds items:items];
        
        _popMenu.didSelectedItemCompletion=^(MenuItem *item){
            NSLog(@"%@",item);
        };
    }
    return _popMenu;
}

- (void)addCustomTabBar{
    self.tabBar.hidden=YES;//隐藏系统自带tabbar
    self.wbTabBar=[[WBTabBar alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-49, SCREEN_WIDTH, 49)];
    
    self.wbTabBar.delegate=self;
    
    __weak typeof(self) weakSelf=self;
    self.wbTabBar.passBlock=^(NSInteger index){
        weakSelf.selectedIndex=index;
    };
    
    self.wbTabBar.plutBtnPressedBlock=^{
        //显示界面
        [weakSelf.popMenu showMenuAtView:weakSelf.view];
    };
    
    [self.view addSubview:self.wbTabBar];
    self.wbTabBar.backgroundColor=[UIColor whiteColor];
}

//添加视图控制器
- (void)addViewControllers{
    NSArray *vcNameArray=@[@"HomeTableViewController",
                           @"MessageTableViewController",
                           @"DiscoveryTableViewController",
                           @"PersonalTableViewController"];
    NSArray *vcTitleArray=@[@"首页",@"消息",@"发现",@"我"];
    //普通状态图片
    NSArray *norImageArray=@[@"tabbar_home",
                             @"tabbar_message_center",
                             @"tabbar_discover",
                             @"tabbar_profile"];
    //高亮状态图片
    NSArray *higlImageArray=@[@"tabbar_home_highlighted",
                             @"tabbar_message_center_highlighted",
                             @"tabbar_discover_highlighted",
                             @"tabbar_profile_highlighted"];
    //选中状态图片
    NSArray *selImageArray=@[@"tabbar_home_selected",
                             @"tabbar_message_center_selected",
                             @"tabbar_discover_selected",
                             @"tabbar_profile_selected"];
    
    for (NSString *vcName in vcNameArray) {
        NSInteger index=[vcNameArray indexOfObject:vcName];
        Class vcClass=NSClassFromString(vcName);
        UITableViewController *vc=[[vcClass alloc]init];
        vc.title=vcTitleArray[index];
        //修改字体
        NSDictionary *norAttribute=@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor lightGrayColor]};
        NSDictionary *selAttribute=@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor orangeColor]};
        [vc.tabBarItem setTitleTextAttributes:norAttribute forState:UIControlStateNormal];
        [vc.tabBarItem setTitleTextAttributes:selAttribute forState:UIControlStateSelected];
        [vc.tabBarItem setImage:[[UIImage imageNamed:norImageArray[index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:selImageArray[index]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        UINavigationController *navc=[[UINavigationController alloc]initWithRootViewController:vc];
        [self addChildViewController:navc];
        
        self.wbTabBar.tabBarItem=vc.tabBarItem;
        
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - WBtabBarDelegate
- (void)passIndex:(NSInteger)index{
    self.selectedIndex=index;
}

@end
